﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;

namespace ACPLogAnalyzer
{
    /// <summary>
    /// Interaction logic for ConfigPlot.xaml
    /// </summary>
    public partial class ConfigPlot : Window
    {
        #region Member vars
        private bool m_init = false;
        public bool m_restartConfig = false;
        public int m_restartTab;

        private List<KeyValuePair<object, object>> m_grDataSeries1;
        private List<KeyValuePair<object, object>> m_grDataSeries2;
        private List<KeyValuePair<object, object>> m_grDataSeries3;

        private SolidColorBrush m_fgBrush;
        private SolidColorBrush m_fgBrushTitle;
        private SolidColorBrush m_bgBrushGraphWnd;
        private SolidColorBrush m_bgBrushKey;
        private SolidColorBrush m_brushMainLine;
        private SolidColorBrush m_brushSecondaryLine;
        private SolidColorBrush m_brushTertiaryLine;

        private FontFamily m_fontFamily;
        private FontFamily m_fontFamilyTitle;

        private string m_fontName;
        private string m_fontNameTitle;

        private double m_fontSize;
        public double PlotFontSize
        {
            get { return m_fontSize; }
            set { m_fontSize = value; }
        }

        private double m_fontSizeTitle;
        public double PlotFontSizeTitle
        {
            get { return m_fontSizeTitle; }
            set { m_fontSizeTitle = value; }
        }

        private FontWeight m_fontWeight;
        public FontWeight PlotFontWeight
        {
            get { return m_fontWeight; }
            set { m_fontWeight = value; }
        }

        private FontWeight m_fontWeightTitle;
        public FontWeight PlotFontWeightTitle
        {
            get { return m_fontWeightTitle; }
            set { m_fontWeightTitle = value; }
        }

        private FontStyle m_fontStyle;
        public FontStyle PlotFontStyle
        {
            get { return m_fontStyle; }
            set { m_fontStyle = value; }
        }

        private FontStyle m_fontStyleTitle;
        public FontStyle PlotFontStyleTitle
        {
            get { return m_fontStyleTitle; }
            set { m_fontStyleTitle = value; }
        }
        #endregion

        public ConfigPlot()
        {
            InitializeComponent();
        }

        private void InitText()
        {
            FontWeightConverter fwc = new FontWeightConverter();
            FontStyleConverter fsc = new FontStyleConverter();

            FontSelector.ItemsSource = Fonts.SystemFontFamilies;
            int fontIndex = -1;

            // Font name...
            if (string.IsNullOrEmpty(Properties.Settings.Default.Plot_Font_Name))
            {
                Graph g = new Graph(null, null);
                m_fontName = g.FontFamily.Source;
                Properties.Settings.Default.Plot_Font_Name = m_fontName;
            }
            else
                m_fontName = Properties.Settings.Default.Plot_Font_Name;

            // Font size...
            if (string.IsNullOrEmpty(Properties.Settings.Default.Plot_Font_Size))
                m_fontSize = 12;
            else
            {
                try
                {
                    m_fontSize = double.Parse(Properties.Settings.Default.Plot_Font_Size);
                }
                catch
                {
                }
            }

            // Font weight...
            if (string.IsNullOrEmpty(Properties.Settings.Default.Plot_Font_Weight))
                m_fontWeight = (FontWeight)fwc.ConvertFromString("Normal");
            else
                m_fontWeight = (FontWeight)fwc.ConvertFromString(Properties.Settings.Default.Plot_Font_Weight);

            // Font style...
            if (string.IsNullOrEmpty(Properties.Settings.Default.Plot_Font_Style))
                m_fontStyle = (FontStyle)fsc.ConvertFromString("Normal");
            else
                m_fontStyle = (FontStyle)fsc.ConvertFromString(Properties.Settings.Default.Plot_Font_Style);

            foreach (System.Windows.Media.FontFamily ff in FontSelector.Items)
            {
                fontIndex++;
                if (ff.Source.CompareTo(m_fontName) == 0)
                {
                    m_fontFamily = ff;
                    break;
                }
            }

            // Font FG color
            if (Properties.Settings.Default.Plot_FG_Color != null)
            {
                m_fgBrush = new SolidColorBrush(
                    System.Windows.Media.Color.FromRgb(
                        Properties.Settings.Default.Plot_FG_Color.R,
                        Properties.Settings.Default.Plot_FG_Color.G,
                        Properties.Settings.Default.Plot_FG_Color.B));
            }
            else
                m_fgBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 0, 0));

            // Init the controls
            FontSelector.SelectedIndex = fontIndex;
            comboBoxStyle.SelectedValue = fsc.ConvertToString((FontStyle)m_fontStyle);
            comboBoxWeight.SelectedValue = fwc.ConvertToString((FontWeight)m_fontWeight);
            sliderFontSize.DataContext = this;
        }

        private void InitTextTitle()
        {
            FontWeightConverter fwc = new FontWeightConverter();
            FontStyleConverter fsc = new FontStyleConverter();

            FontSelectorTitle.ItemsSource = Fonts.SystemFontFamilies;
            int fontIndex = -1;

            // Font name...
            if (string.IsNullOrEmpty(Properties.Settings.Default.Plot_Font_Name_Title))
            {
                Graph g = new Graph(null, null);
                m_fontNameTitle = g.FontFamily.Source;
                Properties.Settings.Default.Plot_Font_Name_Title = m_fontNameTitle;
            }
            else
                m_fontNameTitle = Properties.Settings.Default.Plot_Font_Name_Title;

            // Font size...
            if (string.IsNullOrEmpty(Properties.Settings.Default.Plot_Font_Size_Title))
                m_fontSizeTitle = 12;
            else
            {
                try
                {
                    m_fontSizeTitle = double.Parse(Properties.Settings.Default.Plot_Font_Size_Title);
                }
                catch
                {
                }
            }

            // Font weight...
            if (string.IsNullOrEmpty(Properties.Settings.Default.Plot_Font_Weight_Title))
                m_fontWeightTitle = (FontWeight)fwc.ConvertFromString("Normal");
            else
                m_fontWeightTitle = (FontWeight)fwc.ConvertFromString(Properties.Settings.Default.Plot_Font_Weight_Title);

            // Font style...
            if (string.IsNullOrEmpty(Properties.Settings.Default.Plot_Font_Style_Title))
                m_fontStyleTitle = (FontStyle)fsc.ConvertFromString("Normal");
            else
                m_fontStyleTitle = (FontStyle)fsc.ConvertFromString(Properties.Settings.Default.Plot_Font_Style_Title);

            foreach (System.Windows.Media.FontFamily ff in FontSelectorTitle.Items)
            {
                fontIndex++;
                if (ff.Source.CompareTo(m_fontNameTitle) == 0)
                {
                    m_fontFamilyTitle = ff;
                    break;
                }
            }

            // Font FG color
            if (Properties.Settings.Default.Plot_FG_Color_Title != null)
            {
                m_fgBrushTitle = new SolidColorBrush(
                    System.Windows.Media.Color.FromRgb(
                        Properties.Settings.Default.Plot_FG_Color_Title.R,
                        Properties.Settings.Default.Plot_FG_Color_Title.G,
                        Properties.Settings.Default.Plot_FG_Color_Title.B));
            }
            else
                m_fgBrushTitle = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 0, 0));

            // Init the controls
            FontSelectorTitle.SelectedIndex = fontIndex;
            comboBoxStyleTitle.SelectedValue = fsc.ConvertToString((FontStyle)m_fontStyleTitle);
            comboBoxWeightTitle.SelectedValue = fwc.ConvertToString((FontWeight)m_fontWeightTitle);
            sliderFontSizeTitle.DataContext = this;
        }

        private void InitGraphColors()
        {
            // Graph BG color
            if (Properties.Settings.Default.Plot_BG_Graph != null)
            {
                m_bgBrushGraphWnd = new SolidColorBrush(
                    System.Windows.Media.Color.FromRgb(
                        Properties.Settings.Default.Plot_BG_Graph.R,
                        Properties.Settings.Default.Plot_BG_Graph.G,
                        Properties.Settings.Default.Plot_BG_Graph.B));
            }
            else
                m_bgBrushGraphWnd = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 255, 255));

            // Graph Key BG color
            if (Properties.Settings.Default.Plot_BG_Key != null)
            {
                m_bgBrushKey = new SolidColorBrush(
                    System.Windows.Media.Color.FromRgb(
                        Properties.Settings.Default.Plot_BG_Key.R,
                        Properties.Settings.Default.Plot_BG_Key.G,
                        Properties.Settings.Default.Plot_BG_Key.B));
            }
            else
                m_bgBrushKey = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 255, 255));

            // Graph Key transparency
            if (Properties.Settings.Default.Plot_BG_Key_Transparent)
                checkBoxKeyTransparent.IsChecked = true;
            else
                checkBoxKeyTransparent.IsChecked = false;
        }

        private void InitSeriesStyles()
        {
            textBoxPrimaryThickness.Text = Properties.Settings.Default.Plot_Main_Thickness.ToString();
            textBoxSecondaryThickness.Text = Properties.Settings.Default.Plot_Secondary_Thickness.ToString();
            textBoxTertiaryThickness.Text = Properties.Settings.Default.Plot_Tertiary_Thickness.ToString();

            checkBoxShowPointsMain.IsChecked = Properties.Settings.Default.Plot_Main_Show_Data_Points;
            checkBoxSecondaryShowDataPoints.IsChecked = Properties.Settings.Default.Plot_Secondary_Show_Data_Points;
            checkBoxTertiaryShowDataPoints.IsChecked = Properties.Settings.Default.Plot_Tertiary_Show_Data_Points;

            if (Properties.Settings.Default.Plot_Main_Line_Color != null)
            {
                m_brushMainLine = new SolidColorBrush(
                    System.Windows.Media.Color.FromRgb(
                        Properties.Settings.Default.Plot_Main_Line_Color.R,
                        Properties.Settings.Default.Plot_Main_Line_Color.G,
                        Properties.Settings.Default.Plot_Main_Line_Color.B));
            }
            else
                m_brushMainLine = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 0, 255));

            if (Properties.Settings.Default.Plot_Secondary_Line_Color != null)
            {
                m_brushSecondaryLine = new SolidColorBrush(
                    System.Windows.Media.Color.FromRgb(
                        Properties.Settings.Default.Plot_Secondary_Line_Color.R,
                        Properties.Settings.Default.Plot_Secondary_Line_Color.G,
                        Properties.Settings.Default.Plot_Secondary_Line_Color.B));
            }
            else
                m_brushSecondaryLine = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 0, 0));

            if (Properties.Settings.Default.Plot_Tertiary_Line_Color != null)
            {
                m_brushTertiaryLine = new SolidColorBrush(
                    System.Windows.Media.Color.FromRgb(
                        Properties.Settings.Default.Plot_Tertiary_Line_Color.R,
                        Properties.Settings.Default.Plot_Tertiary_Line_Color.G,
                        Properties.Settings.Default.Plot_Tertiary_Line_Color.B));
            }
            else
                m_brushTertiaryLine = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 255, 0));
        }

        private void InitUIControls()
        {
            // Graph text controls...
            textBlockExample.FontFamily = m_fontFamily;
            textBlockExample.FontSize = m_fontSize;
            textBlockExample.FontWeight = m_fontWeight;
            textBlockExample.FontStyle = m_fontStyle;
            textBlockExample.Foreground = m_fgBrush;
            labelFgColor.Background = m_fgBrush;

            // Graph title controls...
            textBlockExampleTitle.FontFamily = m_fontFamilyTitle;
            textBlockExampleTitle.FontSize = m_fontSizeTitle;
            textBlockExampleTitle.FontWeight = m_fontWeightTitle;
            textBlockExampleTitle.FontStyle = m_fontStyleTitle;
            textBlockExampleTitle.Foreground = m_fgBrushTitle;
            labelFgColorTitle.Background = m_fgBrushTitle;

            // Graph bg color control...
            labelGraphWndBg.Background = m_bgBrushGraphWnd;

            // Graph key bg color control...
            labelKeyBg.Background = m_bgBrushKey;

            // Data series line colors
            labelPrimaryColor.Background = m_brushMainLine;
            labelSecondaryColor.Background = m_brushSecondaryLine;
            labelTertiaryColor.Background = m_brushTertiaryLine;
        }

        private void InitGraphData()
        {
            m_grDataSeries1 = new List<KeyValuePair<object, object>>();
            m_grDataSeries2 = new List<KeyValuePair<object, object>>();
            m_grDataSeries3 = new List<KeyValuePair<object, object>>();

            graphUC.DataSourceSeries1 = m_grDataSeries1;
            graphUC.DataSourceSeries2 = m_grDataSeries2;
            graphUC.DataSourceSeries3 = m_grDataSeries3;

            m_grDataSeries1.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 10, 0).TimeOfDay, 5.1));
            m_grDataSeries1.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 20, 0).TimeOfDay, 3.2));
            m_grDataSeries1.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 30, 0).TimeOfDay, 8.4));
            m_grDataSeries1.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 40, 0).TimeOfDay, 9.5));
            m_grDataSeries1.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 50, 0).TimeOfDay, 2.2));

            m_grDataSeries2.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 10, 0).TimeOfDay, 5.1));
            m_grDataSeries2.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 20, 0).TimeOfDay, 5.1));
            m_grDataSeries2.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 30, 0).TimeOfDay, 5.1));
            m_grDataSeries2.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 40, 0).TimeOfDay, 5.1));
            m_grDataSeries2.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 50, 0).TimeOfDay, 5.1));

            m_grDataSeries3.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 05, 0).TimeOfDay, 3.2));
            m_grDataSeries3.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 15, 0).TimeOfDay, 3.7));
            m_grDataSeries3.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 25, 0).TimeOfDay, 4.4));
            m_grDataSeries3.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 35, 0).TimeOfDay, 6.5));
            m_grDataSeries3.Add(new KeyValuePair<object, object>(new DateTime(2011, 11, 22, 1, 45, 0).TimeOfDay, 1.6));

            graphUC.ShowSecondarySeries(true);
            graphUC.RefreshData();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (m_restartConfig)
                tabControl1.SelectedIndex = m_restartTab;

            m_restartConfig = false;
            m_restartTab = 0;

            InitText();
            InitTextTitle();
            InitGraphColors();
            InitSeriesStyles();
            InitGraphData();
            graphUC.UpdateStyles();
            InitUIControls();

            m_init = true;
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        private void FontSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            m_fontFamily = (FontFamily)FontSelector.SelectedValue;
            textBlockExample.FontFamily = m_fontFamily;
            Properties.Settings.Default.Plot_Font_Name = m_fontFamily.Source;
            graphUC.UpdateStyles();
        }

        private void FontSelectorTitle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            m_fontFamilyTitle = (FontFamily)FontSelectorTitle.SelectedValue;
            textBlockExampleTitle.FontFamily = m_fontFamilyTitle;
            Properties.Settings.Default.Plot_Font_Name_Title = m_fontFamilyTitle.Source;
            graphUC.UpdateStyles();
        }

        private void sliderFontSize_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!m_init || e.NewValue < 5)  
                return;

            textBlockExample.FontSize = e.NewValue;
            Properties.Settings.Default.Plot_Font_Size = e.NewValue.ToString();
            graphUC.UpdateStyles();
        }

        private void sliderFontSizeTitle_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!m_init || e.NewValue < 5)
                return;

            textBlockExampleTitle.FontSize = e.NewValue;
            Properties.Settings.Default.Plot_Font_Size_Title = e.NewValue.ToString();
            graphUC.UpdateStyles();
        }

        private void comboBoxWeight_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBoxWeight.SelectedValue == null)
                return;

            string fontWeight = (string)comboBoxWeight.SelectedValue;
            FontWeightConverter fwc = new FontWeightConverter();
            m_fontWeight = (FontWeight)fwc.ConvertFromString(fontWeight);
            textBlockExample.FontWeight = m_fontWeight;
            Properties.Settings.Default.Plot_Font_Weight = fontWeight;
            graphUC.UpdateStyles();
        }

        private void comboBoxWeightTitle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBoxWeightTitle.SelectedValue == null)
                return;

            string fontWeight = (string)comboBoxWeightTitle.SelectedValue;
            FontWeightConverter fwc = new FontWeightConverter();
            m_fontWeightTitle = (FontWeight)fwc.ConvertFromString(fontWeight);
            textBlockExampleTitle.FontWeight = m_fontWeightTitle;
            Properties.Settings.Default.Plot_Font_Weight_Title = fontWeight;
            graphUC.UpdateStyles();
        }

        private void comboBoxStyle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBoxStyle.SelectedValue == null)
                return;

            string fontStyle = (string)comboBoxStyle.SelectedValue;
            FontStyleConverter fsc = new FontStyleConverter();
            m_fontStyle = (FontStyle)fsc.ConvertFromString(fontStyle);
            textBlockExample.FontStyle = m_fontStyle;
            Properties.Settings.Default.Plot_Font_Style = fontStyle;
            graphUC.UpdateStyles();
        }

        private void comboBoxStyleTitle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBoxStyleTitle.SelectedValue == null)
                return;

            string fontStyle = (string)comboBoxStyleTitle.SelectedValue;
            FontStyleConverter fsc = new FontStyleConverter();
            m_fontStyleTitle = (FontStyle)fsc.ConvertFromString(fontStyle);
            textBlockExampleTitle.FontStyle = m_fontStyleTitle;
            Properties.Settings.Default.Plot_Font_Style_Title = fontStyle;
            graphUC.UpdateStyles();
        }

        private void labelFgColor_MouseUp(object sender, MouseButtonEventArgs e)
        {
            using (System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog())
            { 
                cd.AllowFullOpen = true;
                if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    m_fgBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(cd.Color.R, cd.Color.G, cd.Color.B));
                    Properties.Settings.Default.Plot_FG_Color = System.Drawing.Color.FromArgb(cd.Color.R, cd.Color.G, cd.Color.B);
                    labelFgColor.Background = m_fgBrush;
                    textBlockExample.Foreground = m_fgBrush;
                    graphUC.UpdateStyles();
                }
            }
        }

        private void labelFgColorTitle_MouseUp(object sender, MouseButtonEventArgs e)
        {
            using (System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog())
            {
                cd.AllowFullOpen = true;
                if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    m_fgBrushTitle = new SolidColorBrush(System.Windows.Media.Color.FromRgb(cd.Color.R, cd.Color.G, cd.Color.B));
                    Properties.Settings.Default.Plot_FG_Color_Title = System.Drawing.Color.FromArgb(cd.Color.R, cd.Color.G, cd.Color.B);
                    labelFgColorTitle.Background = m_fgBrushTitle;
                    textBlockExampleTitle.Foreground = m_fgBrushTitle;
                    graphUC.UpdateStyles();
                }
            }
        }

        private void labelGraphWndBg_MouseUp(object sender, MouseButtonEventArgs e)
        {
            using (System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog())
            {
                cd.AllowFullOpen = true;
                if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    m_bgBrushGraphWnd = new SolidColorBrush(System.Windows.Media.Color.FromRgb(cd.Color.R, cd.Color.G, cd.Color.B));
                    Properties.Settings.Default.Plot_BG_Graph = System.Drawing.Color.FromArgb(cd.Color.R, cd.Color.G, cd.Color.B);
                    labelGraphWndBg.Background = m_bgBrushGraphWnd;
                    graphUC.UpdateStyles();
                }
            }
        }

        private void labelKeyBg_MouseUp(object sender, MouseButtonEventArgs e)
        {
            using (System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog())
            {
                cd.AllowFullOpen = true;
                if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    m_bgBrushKey = new SolidColorBrush(System.Windows.Media.Color.FromRgb(cd.Color.R, cd.Color.G, cd.Color.B));
                    Properties.Settings.Default.Plot_BG_Key = System.Drawing.Color.FromArgb(cd.Color.R, cd.Color.G, cd.Color.B);
                    labelKeyBg.Background = m_bgBrushKey;
                    graphUC.UpdateStyles();
                }
            }
        }

        private void checkBoxKeyTransparent_Checked(object sender, RoutedEventArgs e)
        {
            if (!m_init)
                return;

            Properties.Settings.Default.Plot_BG_Key_Transparent = true;
            graphUC.UpdateStyles();
        }

        private void checkBoxKeyTransparent_Unchecked(object sender, RoutedEventArgs e)
        {
            if (!m_init)
                return;

            Properties.Settings.Default.Plot_BG_Key_Transparent = false;
            graphUC.UpdateStyles();
        }

        private void labelPrimaryColor_MouseUp(object sender, MouseButtonEventArgs e)
        {
            using (System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog())
            {
                cd.AllowFullOpen = true;
                if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    m_brushMainLine = new SolidColorBrush(System.Windows.Media.Color.FromRgb(cd.Color.R, cd.Color.G, cd.Color.B));
                    Properties.Settings.Default.Plot_Main_Line_Color = System.Drawing.Color.FromArgb(cd.Color.R, cd.Color.G, cd.Color.B);
                    labelPrimaryColor.Background = m_brushMainLine;
                    m_restartConfig = true;
                    m_restartTab = 3;  // Hack: Can't get the example graph line colors to update unless we destroy the wnd and re-open
                    this.Close();
                }
            }
        }

        private void labelSecondaryColor_MouseUp(object sender, MouseButtonEventArgs e)
        {
            using (System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog())
            {
                cd.AllowFullOpen = true;
                if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    m_brushSecondaryLine = new SolidColorBrush(System.Windows.Media.Color.FromRgb(cd.Color.R, cd.Color.G, cd.Color.B));
                    Properties.Settings.Default.Plot_Secondary_Line_Color = System.Drawing.Color.FromArgb(cd.Color.R, cd.Color.G, cd.Color.B);
                    labelSecondaryColor.Background = m_brushSecondaryLine;
                    m_restartConfig = true;
                    m_restartTab = 4;  // Hack: Can't get the example graph line colors to update unless we destroy the wnd and re-open
                    this.Close();
                }
            }
        }

        private void labelTertiaryColor_MouseUp(object sender, MouseButtonEventArgs e)
        {
            using (System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog())
            {
                cd.AllowFullOpen = true;
                if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    m_brushTertiaryLine = new SolidColorBrush(System.Windows.Media.Color.FromRgb(cd.Color.R, cd.Color.G, cd.Color.B));
                    Properties.Settings.Default.Plot_Tertiary_Line_Color = System.Drawing.Color.FromArgb(cd.Color.R, cd.Color.G, cd.Color.B);
                    labelTertiaryColor.Background = m_brushTertiaryLine;
                    m_restartConfig = true;
                    m_restartTab = 5;  // Hack: Can't get the example graph line colors to update unless we destroy the wnd and re-open
                    this.Close();
                }
            }
        }

        private void textBoxPrimaryThickness_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!m_init)
                return;

            try
            {
                double d = double.Parse(textBoxPrimaryThickness.Text);
                Properties.Settings.Default.Plot_Main_Thickness = d;
                graphUC.UpdateStyles();
            }
            catch
            {
            }
        }

        private void textBoxSecondaryThickness_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!m_init)
                return;

            try
            {
                double d = double.Parse(textBoxSecondaryThickness.Text);
                Properties.Settings.Default.Plot_Secondary_Thickness = d;
                graphUC.UpdateStyles();
            }
            catch
            {
            }
        }

        private void textBoxTertiaryThickness_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!m_init)
                return;

            try
            {
                double d = double.Parse(textBoxTertiaryThickness.Text);
                Properties.Settings.Default.Plot_Tertiary_Thickness = d;
                graphUC.UpdateStyles();
            }
            catch
            {
            }
        }

        private void checkBoxShowPointsMain_Checked(object sender, RoutedEventArgs e)
        {
            if (!m_init)
                return;

            Properties.Settings.Default.Plot_Main_Show_Data_Points = true;
            m_restartConfig = true;
            m_restartTab = 3;  // Hack: Can't get the example graph line datapoints to update unless we destroy the wnd and re-open
            this.Close();
        }

        private void checkBoxShowPointsMain_Unchecked(object sender, RoutedEventArgs e)
        {
            if (!m_init)
                return;

            Properties.Settings.Default.Plot_Main_Show_Data_Points = false;
            m_restartConfig = true;
            m_restartTab = 3;  // Hack: Can't get the example graph line datapoints to update unless we destroy the wnd and re-open
            this.Close();
        }

        private void checkBoxSecondaryShowDataPoints_Checked(object sender, RoutedEventArgs e)
        {
            if (!m_init)
                return;

            Properties.Settings.Default.Plot_Secondary_Show_Data_Points = true;
            m_restartConfig = true;
            m_restartTab = 4;  // Hack: Can't get the example graph line datapoints to update unless we destroy the wnd and re-open
            this.Close();
        }

        private void checkBoxSecondaryShowDataPoints_Unchecked(object sender, RoutedEventArgs e)
        {
            if (!m_init)
                return;

            Properties.Settings.Default.Plot_Secondary_Show_Data_Points = false;
            m_restartConfig = true;
            m_restartTab = 4;  // Hack: Can't get the example graph line datapoints to update unless we destroy the wnd and re-open
            this.Close();
        }

        private void checkBoxTertiaryShowDataPoints_Checked(object sender, RoutedEventArgs e)
        {
            if (!m_init)
                return;

            Properties.Settings.Default.Plot_Tertiary_Show_Data_Points = true;
            m_restartConfig = true;
            m_restartTab = 5;  // Hack: Can't get the example graph line datapoints to update unless we destroy the wnd and re-open
            this.Close();
        }

        private void checkBoxTertiaryShowDataPoints_Unchecked(object sender, RoutedEventArgs e)
        {
            if (!m_init)
                return;

            Properties.Settings.Default.Plot_Tertiary_Show_Data_Points = false;
            m_restartConfig = true;
            m_restartTab = 5;  // Hack: Can't get the example graph line datapoints to update unless we destroy the wnd and re-open
            this.Close();
        }

    }
}
